<html>
<head>
<title>MEMORY INFO</title>
<meta charset="UTF-8">
<script src="jquery-3.6.0.min.js"></script>
<script>
function testShowSize(size) {
    let b = size;
    let k = b / 1024;
    let m = k / 1024;
    let g = m / 1024;
    if (g > 1) {
        return g.toFixed(2) + "GB";
    }
    if (m > 1) {
        return m.toFixed(2) + "MB";
    }
    if (k > 1) {
        return k.toFixed(2) + "KB";
    }
    return b + "B";
}
function success() {
    $.get("memory.php", {}, function(data, textStatus, jqXHR) {
        let tTotal = $("#t1");
        let tUsedTotal = $("#tused1");
        let tUsedPer = $("#tused1p");
        let tFreeTotal = $("#tfree1");
        let tFreePer = $("#tfree1p");

        let phyTotal = $("#total1");
        let usedTotal = $("#used1");
        let usedPer = $("#used1p");
        let freeTotal = $("#free1");
        let freePer = $("#free1p");
        let buffersTotal = $("#buffers1");
        let buffersPer = $("#buffers1p");
        let cacheTotal = $("#cache1");
        let cachePer = $("#cache1p");
        let sharedTotal = $("#shared1");
        let sharedPer = $("#shared1p");
        let availableTotal = $("#available1");
        let availablePer = $("#available1p");

        let swapTotal = $("#swap1");
        let swapUsedTotal = $("#swapused1");
        let swapUsedPer = $("#swapused1p");
        let swapFreeTotal = $("#swapfree1");
        let swapFreePer = $("#swapfree1p");

        if ("success" === textStatus) {
            let basicInfo = data[0];
            let swapInfo = false;
            if (data[1]) {
                swapInfo = data[1];
            }

            let tTotalValue = basicInfo.used + basicInfo.free + (swapInfo ? (swapInfo.used + swapInfo.free) : 0);
            let tUsedValue = basicInfo.used + (swapInfo ? swapInfo.used : 0);
            let tFreeValue = basicInfo.free + (swapInfo ? swapInfo.free : 0);
            tTotal.text(testShowSize(tTotalValue));
            tUsedTotal.text(testShowSize(tUsedValue));
            tUsedPer.text((tUsedValue / tTotalValue * 100).toFixed(2));
            tFreeTotal.text(testShowSize(tFreeValue));
            tFreePer.text((tFreeValue / tTotalValue * 100).toFixed(2));

            phyTotal.text(testShowSize(basicInfo.total));
            usedTotal.text(testShowSize(basicInfo.used));
            usedPer.text((basicInfo.used / basicInfo.total * 100).toFixed(2));
            freeTotal.text(testShowSize(basicInfo.free));
            freePer.text((basicInfo.free / basicInfo.total * 100).toFixed(2));
            buffersTotal.text(testShowSize(basicInfo.buffers));
            buffersPer.text((basicInfo.buffers / basicInfo.total * 100).toFixed(2));
            cacheTotal.text(testShowSize(basicInfo.cache));
            cachePer.text((basicInfo.cache / basicInfo.total * 100).toFixed(2));
            sharedTotal.text(testShowSize(basicInfo.shared));
            sharedPer.text((basicInfo.shared / basicInfo.cache * 100).toFixed(2));
            if (basicInfo.available) {
                availableTotal.text(testShowSize(basicInfo.available));
                availablePer.text((basicInfo.available / (basicInfo.free + basicInfo.cache) * 100).toFixed(2));
            }

            if (swapInfo) {
                swapTotal.text(testShowSize(swapInfo.total));
                swapUsedTotal.text(testShowSize(swapInfo.used));
                swapUsedPer.text((swapInfo.used / swapInfo.total * 100).toFixed(2));
                swapFreeTotal.text(testShowSize(swapInfo.free));
                swapFreePer.text((swapInfo.free / swapInfo.total * 100).toFixed(2));
            }
            window.setTimeout(success, 500);
        }
    });
}
$(function() {
    success();
});
</script>
</head>
<body>
    <h1>内存统计信息</h1>
    <h2>综合汇总</h2>
    <table border="1" cellspacing="0">
        <tr><th>项目</th><th>总体大小</th><th>比例</th><th>备注</th></tr>
        <tr><td>综合空间</td><td><span id="t1"></span></td><td>100.00%</td><td>所有的使用中加未使用的空间。</td></tr>
        <tr><td>使用中</td><td><span id="tused1"></span></td><td><span id="tused1p"></span>%</td><td>所有已使用空间。</td></tr>
        <tr><td>未使用</td><td><span id="tfree1"></span></td><td><span id="tfree1p"></span>%</td><td>所有未使用空间。</td></tr>
    </table>
    <ul>
        <li>物理内存和交换分区的信息综合。</li>
        <li>你可以从下面的表格获得具体的物理内存和交换分区的信息。</li>
    </ul>
    <h2>物理内存</h2>
    <table border="1" cellspacing="0">
        <tr><th>项目</th><th>占用内存大小</th><th>比例</th><th>备注</th></tr>
        <tr><td>物理内存</td><td><span id="total1"></span></td><td>100.00%</td><td>排除为硬件和系统保留的内存空间之外的物理内存总量。</td></tr>
        <tr><td>使用中</td><td><span id="used1"></span></td><td><span id="used1p"></span>%</td><td>物理内存中存储的进程占用的内存。</td></tr>
        <tr><td>未使用</td><td><span id="free1"></span></td><td><span id="free1p"></span>%</td><td>没有被使用的内存。</td></tr>
        <tr><td>磁盘缓冲</td><td><span id="buffers1"></span></td><td><span id="buffers1p"></span>%</td><td>缓存的用于写入磁盘的被修改过的数据。</td></tr>
        <tr><td>缓存</td><td><span id="cache1"></span></td><td><span id="cache1p"></span>%</td><td>从磁盘读取的缓存数据和可在进程间共享的内存数据等。</td></tr>
        <tr><td>已共享</td><td><span id="shared1"></span></td><td><span id="shared1p"></span>%</td><td>可在进程间共享的内存数据(如/dev/shm)及其在缓存中占用的百分比。</td></tr>
        <tr><td>可用内存</td><td><span id="available1"></span></td><td><span id="available1p"></span>%</td><td>为了运行程序物理内存最多能腾出的内存及其在“未使用 + 缓存”中占用的百分比。</td></tr>
    </table>
    <ul>
        <li>物理内存 = 使用中 + 未使用 + 磁盘缓冲 + 缓存 。</li>
        <li>当运行程序需使用多于或者接近可用内存大小的空间时，如果存在交换分区则操作系统将尝试把内存中的不活跃数据存入交换分区内（具体取决于操作系统，你可以修改swapness的值来影响操作系统的判断）。</li>
    </ul>
    <h2>交换分区</h2>
    <table border="1" cellspacing="0">
        <tr><th>项目</th><th>占据交换分区大小</th><th>比例</th><th>备注</th></tr>
        <tr><td>交换分区大小</td><td><span id="swap1"></span></td><td>100.00%</td><td>交换分区的大小。</td></tr>
        <tr><td>使用中</td><td><span id="swapused1"></span></td><td><span id="swapused1p"></span>%</td><td>已使用的交换分区大小。</td></tr>
        <tr><td>未使用</td><td><span id="swapfree1"></span></td><td><span id="swapfree1p"></span>%</td><td>未使用的交换分区大小。</td></tr>
    </table>
    <ul>
        <li>由于交换分区无需腾出部分空间用于磁盘缓冲和缓存，所以：交换分区大小 = 使用中 + 未使用 。</li>
        <li>如果没有开启交换分区，那么交换分区统计量可忽略。</li>
    </ul>
</body>
</html>
